package com.example.provider.log.storage;

import com.example.api.common.entity.OperationLog;
import com.example.api.common.log.LogStorage;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class KafkaLogStorage implements LogStorage {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    @Async
    @Override
    public void store(OperationLog operationLog) {
        try {
            kafkaTemplate.send("operation-logs", objectMapper.writeValueAsString(log));
        } catch (Exception e) {
            log.error("Failed to store log in Kafka", e);
        }
    }
} 